package com.bigdata.schaduler.service;

import com.bigdata.schaduler.client.DolphinClient;
import org.apache.dolphinscheduler.common.enums.HttpMethod;
import org.apache.dolphinscheduler.dao.entity.Project;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

public class ProjectService {

  private final DolphinClient dolphinClient;

  public ProjectService(DolphinClient dolphinClient) {
    this.dolphinClient = dolphinClient;
  }

  public Project create(String projectName, String projectDesc) {
    String uri = "v2/projects";
    MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
    params.add("projectName", projectName);
    params.add("description", projectDesc);
    return dolphinClient.requestFormData(HttpMethod.POST, uri, params, Project.class);
  }

  public Project update(Long projectCode, String projectName, String projectDesc, String userName) {
    String uri = "v2/projects/" + projectCode;
    MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
    params.add("projectName", projectName);
    params.add("description", projectDesc);
    params.add("userName", userName);
    return dolphinClient.requestFormData(HttpMethod.PUT, uri, params, Project.class);
  }

  public void delete(Long projectCode) {
    String uri = "v2/projects/" + projectCode;
    dolphinClient.requestFormData(HttpMethod.DELETE, uri, null, Void.class);
  }

  public Project select(Long projectCode) {
    String uri = "v2/projects/" + projectCode;
    return dolphinClient.requestFormData(HttpMethod.GET, uri, null, Project.class);
  }

}
